package FRC2856.Robot;



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



import com.sun.squawk.microedition.io.*;
import java.io.*;
import java.util.*;
import javax.microedition.io.*;

/**
 *
 * @author ASTEPHENSON
 */
public class SocketHandler {
    private PrintStream out;
    private BufferedReader in;
    private ReboundRumbleBot base;
    private boolean connected;

    /** TODO: Change! */
    public SocketHandler (ReboundRumbleBot robot, String host, int port)
    {
        connected = false;
        System.out.println("INIT lalalala");
        base = robot;
        try
        {
            System.out.println("ServerSocket starting...");
            ServerSocketConnection server = (ServerSocketConnection) Connector.open("socket://" + host + ":" + port);
            SocketConnection sc = (SocketConnection) server.acceptAndOpen();
            
//            sc.setSocketOption(SocketConnection.DELAY, 0);
//            sc.setSocketOption(SocketConnection.LINGER, 0);
//            sc.setSocketOption(SocketConnection.KEEPALIVE, 0);
            sc.setSocketOption(SocketConnection.RCVBUF, 128);
            sc.setSocketOption(SocketConnection.SNDBUF, 128);
            
            System.out.println("Socket bound to port " + port);

           in  = new BufferedReader(sc.openInputStream());
           out = new PrintStream(sc.openOutputStream());
           connected = true;


//           String line = in.readLine();
//           System.out.println("Server wanted me to know: " + line);
       } catch(Exception e) {
           System.out.println("Exception!" + e);
            e.printStackTrace();
       }
    }

    public void startReadThread()
    {
        new Thread("Socket Reader")
        {
            public void run()
            {
                String line;
                
                while(true)
                {
                    if(!connected)
                    {
                        edu.wpi.first.wpilibj.Timer.delay(1);
                        continue;
                    }
                    try {
                        System.out.println("Awaiting Read");
                        line = in.readLine();
                        System.out.println("Read: " + line);

                        if(line.indexOf("SET>") != -1)
                        {
                            String text = line.substring("SET>".length()+1);
                            Vector v = RobotCoding.split(text, ":");

                            String varName  = (String) v.elementAt(0);
                            String varValue = (String) v.elementAt(1);
                            //base.modes.put(varName, varValue);
                        }
                        else if(line.indexOf("RUN>") != -1)
                        {
                            String text = line.substring("RUN>".length()+1);

                            //base.modes.put("cmd", text);
                        }
                        else if(line.indexOf("ITERATE>") != -1)
                        {
                            String text = line.substring("ITERATE>".length()+1);

                            //base.modes.put("periodicCode", text);
                        }
                        else if (line.indexOf("GET") != -1)
                        {
                            System.out.println("getting var");
                            String text = line.substring("GET".length()+1);
                            //Object o = base.modes.get(text);
                            //System.out.println("value of " + text + "= " + o);
                            //out.print(o);
                        }
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        }.start();
    }

    public void println(String s)
    {
        out.println(s);
    }

    public String readLine()
    {
        try
        {
            return in.readLine();
        } catch(Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public PrintStream getOS()
    {
        return out;
    }

    public BufferedReader getIS()
    {
        return in;
    }
}
